package be.kdg.componenten.chatters;

import java.util.*;
import java.io.Serializable;
import java.rmi.RemoteException;

/**
 * Tom Verelst
 * Wim Goeman
 */
public class ChattersImpl implements Chatters, Serializable {

    private Map<String, Chatter> chatters;

    public ChattersImpl()
    {
        chatters = new TreeMap<String, Chatter>();
    }

/**
     * @see Chatters#add
     */
    public void add(Chatter chatter) {
        if (chatter == null) return;
        try{
        chatters.put(chatter.getName(), chatter);
            }catch( RemoteException re){
            re.printStackTrace();
        }
    }

    /**
     * @see Chatters#remove
     */
    public void remove(Chatter chatter) {
        try{
        chatters.remove(chatter.getName());
        }catch( RemoteException re){
            re.printStackTrace();
        }
    }

    /**
     * @see Chatters#contains
     */
    public Boolean contains(String name) {
        return chatters.containsKey(name);
    }

    /**
     * @see Chatters#getNumberOfChatters
     */
    public int getNumberOfChatters() {
       return chatters.size();
    }

    /**
     * @see Chatters#receive
     */
    public void receive(String name, String text){
        try{
        for(Chatter chatter : chatters.values()){
            System.out.println("Sending message to " + chatter.getName());
            chatter.receive(name, text);
        }
        }catch( RemoteException re){
            re.printStackTrace();
        }
    }

    public List<Chatter> getChatters() {
        List list = new ArrayList();
        for(Chatter chatter : chatters.values()){
            list.add(chatter);    
        }
        return list;
    }
}
